package com.sx.manage.service.operation;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.common.util.http.constants.DebitCreditSign;
import com.sx.manage.biz.consumer.info.IMNetworkPersonConsumerBiz;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.util.WebServiceUtils;
import com.sx.model.constants.MAdjustStatus;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.bank.MConsumerBankMapForm;
import com.sx.model.manage.mybatis.form.consumer.MPersonalConsumerNetworkForm;
import com.sx.model.manage.mybatis.form.sys_operation.MInsideAdjustOrderInfoForm;
import com.sx.model.manage.mybatis.vo.bank.MDropDownVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.consumer.MPersonalConsumerNetworkVO;
import com.sx.model.manage.mybatis.vo.consumer.MPersonalConsumerVO;
import com.sx.model.manage.mybatis.vo.operation.MInsideAdjustOrderVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultDropDownListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

/**
 * 系统运维-内部调账
 * 
 * @author zwm
 *
 */
@Service("insideAdjustOrderInfoService")
public class InsideAdjustOrderInfoService {

	//@Resource(name = "accountService")
	//private IAccountServiceInterface accountService;
	@Resource(name = "mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;
	@Resource(name = "mInsideAdjustOrderInfoServiceImpl")
	private IMInsideAdjustOrderInfoService mInsideAdjustOrderInfoServiceImpl;
	// 用户信息新接口
	@Resource(name = "mNetworkPersonConsumerBizImpl")
	public IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl;
	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "webServiceUtils")
	private WebServiceUtils webServiceUtils;

	/**
	 * 查询账户信息
	 * 
	 * @param accountNo
	 * @return
	 */
	public ResultObjectVO<MInsideAdjustOrderVO> getAccountInfoForQuery(MPersonalConsumerNetworkForm form) {
		if (null == form || StringUtils.isBlank(form.getBank_account_no()) || StringUtils.isBlank(form.getTrade_mem_code())) {
			return new ResultObjectVO<>("sj.m.common.query.error");
		}
		log.info("查询子账户信息account_no={},tradeMemCode={}", form.getBank_account_no(), form.getTrade_mem_code());
		//根据子账号查询摊位号
		String accountNo = webServiceUtils.queryAccountNo(webServiceUtils.queryConsumerIdByBankAccountNo(form.getBank_account_no()));
		//CAccountInfoForQuery ca = null;//accountService.getSingleAccountInfo(accountNo, form.getTrade_mem_code());
		MInsideAdjustOrderVO vo = new MInsideAdjustOrderVO();
//		if (null != ca) {
//			vo.setBlance(ca.getAmount());
//			vo.setBankblance(ca.getBank_amount());
//		}
		//查询用户信息
		MPersonalConsumerNetworkVO consumerVO = mNetworkPersonConsumerBizImpl.get_consumer_byId(webServiceUtils.queryConsumerIdByTradeMemCode(form.getTrade_mem_code())).getObjVo();
		if(null!=consumerVO) {
			vo.setLaw_no(consumerVO.getLaw_no());
			vo.setAccount_name(consumerVO.getConsumer_name());
			return new ResultObjectVO<>(vo);
		}else {
			log.info("根据交易会员代码查询用户信息失败trade_mem_code={}",form.getTrade_mem_code());
			return new ResultObjectVO<>(false,null,"用户不存在");
		}
		
		
	}

	@SuppressWarnings("rawtypes")
	public ResultBooleanVO change_account_status(MInsideAdjustOrderInfoForm form) {
		if (null == form.getId()) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		ResultObjectVO resultVO = mInsideAdjustOrderInfoServiceImpl.search_ById(form);
		MInsideAdjustOrderVO mvo = (MInsideAdjustOrderVO) resultVO.getObjVo();
		if (null == mvo) {
			return new ResultBooleanVO("sj.m.common.query.id.error");
		}
		if (!MAdjustStatus.ADJUST_SUCCSS.equals(mvo.getStatus())) {
			return new ResultBooleanVO("sj.m.data.account.status.error");
		}
		MConsumerBankMapForm bankMapForm = new MConsumerBankMapForm();
		bankMapForm.setBank_account_no(mvo.getAccount());
		bankMapForm.setChannel_status(MDataStatusType.DATA_AVAILABLE);
		List<MConsumerBankMapVO> volist = mBaseConsumerBankMapServiceImpl.query_consumer_bank_list(bankMapForm).getData();
		if (null == volist || volist.isEmpty()) {
			log.info("子账户没有绑定银行卡，不能调账mvo={}", mvo);
			return new ResultBooleanVO(false, null, "子账户没有绑定银行卡，不能调账");
		}
		// 控制出金和入金
		BigDecimal changeAmount = null;
		DebitCreditSign account_type = null;
		if ("IN".equals(mvo.getAdjust_type()) && null != mvo.getAmount()) {
			changeAmount = new BigDecimal(mvo.getAmount().toString());
			log.info("入金用户：" + form.getId().toString() + "金额：" + changeAmount);
			account_type = DebitCreditSign.DEBIT;
		} else if ("OUT".equals(mvo.getAdjust_type()) && null != mvo.getAmount()) {
			changeAmount = new BigDecimal(mvo.getAmount().toString());
			account_type = DebitCreditSign.CREDIT;
			log.info("出金用户：" + form.getId().toString() + "金额：" + changeAmount);
		}
		changeAmount = changeAmount.setScale(2, BigDecimal.ROUND_DOWN);
		// 控制调整账户余额
		//accountService.updateAccountItemAmountByAccountNo(form.getId().toString(), mvo.getAccount(), account_type, changeAmount);
		log.info("调账成功");
		return new ResultBooleanVO(true, "sj.m.data.check.account.succeed.error", "调账成功,具体金额以账户余额为准");
	}

	/**
	 * 
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ResultDropDownListVO<MDropDownVO> ajax_bank_account_adjust(MPersonalConsumerNetworkForm form) throws IOException {
		if (StringUtils.isBlank(form.getBank_account_no())) {
			log.info("bank_account_no为空返回空下拉表", form.getBank_account_no());
			return new ResultDropDownListVO<>(new ArrayList<>(), 0, 0, 0);
		}
		List<MPersonalConsumerVO> consumerList = mNetworkPersonConsumerBizImpl.queryConsumerBankForSelect(form).getData();
		// 查询证件号码-平台账户余额-子账户余额-
		List<MDropDownVO> mdlist = new ArrayList<>();
		if (!consumerList.isEmpty()) {
			for (MPersonalConsumerVO consumerVO : consumerList) {
				MDropDownVO mvo = new MDropDownVO();
				mvo.setId(consumerVO.getTradeMemCode());
				mvo.setText(consumerVO.getBank_account_no());
				mdlist.add(mvo);
				/*
				 * ResultObjectVO<MInsideAdjustOrderVO> objResultVO =
				 * this.getAccountInfoForQuery(consumerVO); // 有为空的可能 if (null
				 * != objResultVO && null != objResultVO.getObjVo()) {
				 * MInsideAdjustOrderVO mivo = objResultVO.getObjVo();
				 * 
				 * }
				 */
			}
		}
		ResultDropDownListVO<MDropDownVO> dropdResultVO = new ResultDropDownListVO<>();
		dropdResultVO.setData(mdlist);
		return dropdResultVO;
	}

}
